package ssh.shop.dao.impl;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import ssh.shop.dao.UserDao;
import ssh.shop.entity.ReturnEmployee;
import ssh.shop.entity.User;

import java.util.List;

@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

    @Override
    public User login(User user) {
        String hql = "from User u where u.userid=:userid and u.password=:password";
        return (User) getSession().createQuery(hql) //
                .setString("userid", user.getUserid()) //
                .setString("password", user.getPassword()) //
                .uniqueResult();
    }

    @Override
    public User getUser(String userid) {
//        String hql = "from User u where u.userid=:userid";
//        return (User) getSession().createQuery(hql).setString("userid", userid).uniqueResult();
        String sql = "select * from user where id =1";
        return (User)getSession().createSQLQuery(sql).addEntity(User.class).list();
    }

    @Override
    public List<User> userList() {
//        String hql = "select new list(id,userid,password,pic) from User u";
        String sql = "select * from user u";
//        return getSession().createQuery(hql).list();
        return getSession().createSQLQuery(sql).addEntity("User",User.class)
                .list();
    }

    @Override
    public List<ReturnEmployee> userListB() {
        String sql = "SELECT a.*,b.type,b.name FROM user a,goods b where a.PIC = b.PIC";
        return (List<ReturnEmployee>) getSession().createSQLQuery(sql)
                .setResultTransformer(Transformers.aliasToBean(ReturnEmployee.class));
    }

    @Override
    public List<Object[]> userListObjec() {
        String sql = "SELECT a.*,b.type,b.name FROM user a,goods b where a.PIC = b.PIC";
        return getSession().createSQLQuery(sql).list();
    }

    @Override
    public void queryUser(User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("update user set PASSWORD ='"+user.getPassword()+"'");
        sql.append(",USERID='"+user.getUserid()+"'");
        sql.append(" where ID='"+user.getID()+"'");
        getSession().createSQLQuery(sql.toString()).executeUpdate();
    }

    @Override
    public void saveUser(User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("insert into user(PASSWORD,USERID) values ('"+user.getPassword()+"','"+user.getUserid()+"')");
        getSession().createSQLQuery(sql.toString()).executeUpdate();

    }

    @Override
    public void delUser(User user) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from user where id='"+user.getId()+"'");
        getSession().createSQLQuery(sql.toString()).executeUpdate();
    }

}




















